<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="员工 身份证号" prop="socialSecurityNumber">
      <el-input v-model="dataForm.socialSecurityNumber" placeholder="员工 身份证号"></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="employeeName">
      <el-input v-model="dataForm.employeeName" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="性别 1：男 2：女" prop="sex">
      <el-input v-model="dataForm.sex" placeholder="性别 1：男 2：女"></el-input>
    </el-form-item>
    <el-form-item label="年龄" prop="age">
      <el-input v-model="dataForm.age" placeholder="年龄"></el-input>
    </el-form-item>
    <el-form-item label="电话号码" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="电话号码"></el-input>
    </el-form-item>
    <el-form-item label="职位" prop="position">
      <el-input v-model="dataForm.position" placeholder="职位"></el-input>
    </el-form-item>
    <el-form-item label="工作地点" prop="workPlace">
      <el-input v-model="dataForm.workPlace" placeholder="工作地点"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          socialSecurityNumber: '',
          employeeName: '',
          sex: '',
          age: '',
          phone: '',
          position: '',
          workPlace: ''
        },
        dataRule: {
          socialSecurityNumber: [
            { required: true, message: '员工 身份证号不能为空', trigger: 'blur' }
          ],
          employeeName: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别 1：男 2：女不能为空', trigger: 'blur' }
          ],
          age: [
            { required: true, message: '年龄不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '电话号码不能为空', trigger: 'blur' }
          ],
          position: [
            { required: true, message: '职位不能为空', trigger: 'blur' }
          ],
          workPlace: [
            { required: true, message: '工作地点不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/contract/employee/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.socialSecurityNumber = data.employee.socialSecurityNumber
                this.dataForm.employeeName = data.employee.employeeName
                this.dataForm.sex = data.employee.sex
                this.dataForm.age = data.employee.age
                this.dataForm.phone = data.employee.phone
                this.dataForm.position = data.employee.position
                this.dataForm.workPlace = data.employee.workPlace
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/contract/employee/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'socialSecurityNumber': this.dataForm.socialSecurityNumber,
                'employeeName': this.dataForm.employeeName,
                'sex': this.dataForm.sex,
                'age': this.dataForm.age,
                'phone': this.dataForm.phone,
                'position': this.dataForm.position,
                'workPlace': this.dataForm.workPlace
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
